package net.lahuobao.finance.service.impl.accredit;

import net.lahuobao.finance.dao.AccreditApplyInfoMapper;
import net.lahuobao.finance.dao.AccreditMapper;
import net.lahuobao.finance.entity.accredit.Accredit;
import net.lahuobao.finance.entity.accredit.AccreditApplyInfo;
import net.lahuobao.finance.service.accredit.IAccreditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/1/6.
 */
@Service
public class AccreditServiceImpl implements IAccreditService{


    @Autowired
    private AccreditApplyInfoMapper accreditApplyInfoMapper;

     @Autowired
    private AccreditMapper accreditMapper;

    @Override
    public AccreditApplyInfo getByOrganizationId(Integer organizationId) {

        return accreditApplyInfoMapper.selectByOrganizationId(organizationId);
    }


    @Override
    @Transactional
    public Map<String,Object> quotaAudit(Integer accreditApplyInfoId, String auditorId, String auditorName,String auditMsg) throws Exception {
        Map<String,Object> result = new HashMap<String, Object>();
        AccreditApplyInfo accreditApplyInfo = accreditApplyInfoMapper.selectByPrimaryKey(accreditApplyInfoId);
        if(accreditApplyInfo == null){
            result.put("status",-1);
            result.put("message","申请不存在，审核失败。");
            return result;
        }

        List<Accredit> accredits = accreditMapper.selectAccreditListByOrganizationId(accreditApplyInfo.getOrganizationId());
        if(accredits == null || accredits.size() != 1){
            result.put("status",-1);
            result.put("message","授信账户不存在或存在多个");
            return result;
        }

        Accredit accredit = accredits.get(0);
        if(accredit.getStatus() != 2){
            result.put("status",-1);
            result.put("message","授信账户暂不能进行操作");
            return result;
        }
        accredit = accreditMapper.selectByPrimaryKeyForUpdate(accredit.getAccreditId());
        if(accredit.getTotal().add(accreditApplyInfo.getAmount()).compareTo(BigDecimal.ZERO) == -1){
            result.put("status",-1);
            result.put("message","申请额度不能大于账户授信额度");
            return result;
        }

        if(accredit.getUsable().add(accreditApplyInfo.getAmount()).compareTo(BigDecimal.ZERO) == -1){
            result.put("status",-1);
            result.put("message","申请额度不能大于账户授信可用额度");
            return result;
        }
        accredit.setTotal(accredit.getTotal().add(accreditApplyInfo.getAmount()));
        accredit.setUsable(accredit.getUsable().add(accreditApplyInfo.getAmount()));

        int resultCount1 = accreditMapper.updateTollAndUsableById(accredit);
        if(resultCount1 > 1){throw new Exception("账户异常");}


        accreditApplyInfo.setAuditorId(auditorId);
        accreditApplyInfo.setAuditorName(auditorName);
        accreditApplyInfo.setAuditMsg(auditMsg);
        accreditApplyInfo.setStatus((short)1);
        int resultCount2 = accreditApplyInfoMapper.quotaAuditRecord(accreditApplyInfo);
        if(resultCount2 > 1){throw new Exception("账户异常");}
        result.put("status",1);
        result.put("message","申请审核成功");
        return result;
    }


    @Override
    @Transactional
    public Map<String, Object> reject(Integer accreditApplyInfoId, String auditorId, String auditorName,String auditMsg) throws Exception {
        Map<String,Object> result = new HashMap<String, Object>();
        AccreditApplyInfo accreditApplyInfo = accreditApplyInfoMapper.selectByPrimaryKey(accreditApplyInfoId);
        if(accreditApplyInfo == null){
            result.put("status",-1);
            result.put("message","申请不存在，审核失败。");
            return result;
        }

        accreditApplyInfo.setAuditorId(auditorId);
        accreditApplyInfo.setAuditorName(auditorName);
        accreditApplyInfo.setAuditMsg(auditMsg);
        accreditApplyInfo.setStatus((short)2);
        int resultCount2 = accreditApplyInfoMapper.quotaAuditRecord(accreditApplyInfo);
        if(resultCount2 > 1){throw new Exception("账户异常");}
        result.put("status",1);
        result.put("message","申请驳回成功");
        return result;
    }
}
